﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc.UI;
using System.IO;
using Microhard.Permission.Models;
using Microhard.Permission.MVCFilter;
using System.Web.Security;

namespace Microhard.FMCG.WebUI.Areas.WX.Controllers
{
    public class ImageBrowserController : EditorFileBrowserController
    {
        private const string contentFolderRoot = "~/Content/";
        private const string prettyName = "UserImages/";
        private static readonly string[] foldersToCopy = new[] { "~/Content/UserImages" };

        /// <summary>
        /// Gets the base paths from which content will be served.
        /// </summary>
        public override string[] ContentPaths
        {
            get
            {
                return new[] { CreateUserFolder() };
            }
        }

        private string UserID
        {
            get
            {
                var user = Membership.GetUser();
                return user.UserName;
            }
        }

        private string CreateUserFolder()
        {
            var userFolder = Path.Combine("UserFiles", UserID);
            var virtualPath = Path.Combine(contentFolderRoot, userFolder, prettyName);
            var path = Server.MapPath(virtualPath);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                foreach (var sourceFolder in foldersToCopy)
                {
                    CopyFolder(Server.MapPath(sourceFolder), path);
                }
            }
            return virtualPath;
        }

        private void CopyFolder(string source, string destination)
        {
            if (!Directory.Exists(destination))
            {
                Directory.CreateDirectory(destination);
            }
            foreach (var file in Directory.EnumerateFiles(source))
            {
                var dest = Path.Combine(destination, Path.GetFileName(file));
                System.IO.File.Copy(file, dest);
            }
            foreach (var folder in Directory.EnumerateDirectories(source))
            {
                var dest = Path.Combine(destination, Path.GetFileName(folder));
                CopyFolder(folder, dest);
            }
        }

        protected override bool CanAccess(string path)
        {
            var user = Membership.GetUser();
            return user == null ? false : true;
        }
    }
}
